// utils/animation-config.js
// 动画配置和主题

const { AnimationUtil } = require('./animation')

/**
 * 动画主题配置
 */
/**
 * 动画主题接口
 * @typedef {Object} AnimationTheme
  name
  durations: {
    fast
    normal
    slow
  }
  easings: {
    easeIn
    easeOut
    easeInOut
    bounce
    elastic
  }
  delays: {
    short
    medium
    long
  }
}

/**
 * 默认动画主题
 */
const defaultTheme = {
  name: 'default',
  durations: {
    fast: 200,
    normal: 300,
    slow: 500
  },
  easings: {
    easeIn: 'ease-in',
    easeOut: 'ease-out',
    easeInOut: 'ease-in-out',
    bounce: 'cubic-bezier(0.68, -0.55, 0.265, 1.55)',
    elastic: 'cubic-bezier(0.175, 0.885, 0.32, 1.275)'
  },
  delays: {
    short: 50,
    medium: 100,
    long: 200
  }
}

/**
 * 情感化动画主题
 */
const emotionalTheme = {
  name: 'emotional',
  durations: {
    fast: 150,
    normal: 400,
    slow: 800
  },
  easings: {
    easeIn: 'cubic-bezier(0.55, 0.085, 0.68, 0.53)',
    easeOut: 'cubic-bezier(0.25, 0.46, 0.45, 0.94)',
    easeInOut: 'cubic-bezier(0.445, 0.05, 0.55, 0.95)',
    bounce: 'cubic-bezier(0.68, -0.55, 0.265, 1.55)',
    elastic: 'cubic-bezier(0.175, 0.885, 0.32, 1.275)'
  },
  delays: {
    short: 30,
    medium: 80,
    long: 150
  }
}

/**
 * 快速动画主题（性能优先）
 */
const performanceTheme = {
  name: 'performance',
  durations: {
    fast: 100,
    normal: 200,
    slow: 300
  },
  easings: {
    easeIn: 'ease-in',
    easeOut: 'ease-out',
    easeInOut: 'ease-in-out',
    bounce: 'ease-out',
    elastic: 'ease-out'
  },
  delays: {
    short: 20,
    medium: 50,
    long: 100
  }
}

/**
 * 动画配置管理器
 */
class AnimationConfig {
  static currentTheme = defaultTheme
  static customThemes = new Map()
  
  /**
   * 设置当前主题
   */
  static setTheme(theme) {
    if (typeof theme === 'string') {
      const customTheme = this.customThemes.get(theme)
      if (customTheme) {
        this.currentTheme = customTheme
      } else {
        console.warn(`Theme '${theme}' not found`)
      }
    } else {
      this.currentTheme = theme
    }
  }
  
  /**
   * 获取当前主题
   */
  static getCurrentTheme() {
    return this.currentTheme
  }
  
  /**
   * 注册自定义主题
   */
  static registerTheme(name, theme) {
    this.customThemes.set(name, theme)
  }
  
  /**
   * 获取持续时间
   */
  static getDuration(speed) {
    return this.currentTheme.durations[speed]
  }
  
  /**
   * 获取缓动函数
   */
  static getEasing(type) {
    return this.currentTheme.easings[type]
  }
  
  /**
   * 获取延迟时间
   */
  static getDelay(length) {
    return this.currentTheme.delays[length]
  }
  
  /**
   * 创建主题化动画
   */
  static createAnimation(config = {}) {
    const duration = typeof config.duration === 'string' 
      ? this.getDuration(config.duration)
      : config.duration || this.getDuration('normal')
    
    const easing = typeof config.easing === 'string' && config.easing in this.currentTheme.easings
      ? this.getEasing(config.easing)
      : config.easing || this.getEasing('easeOut')
    
    const delay = typeof config.delay === 'string'
      ? this.getDelay(config.delay)
      : config.delay || 0
    
    return wx.createAnimation({
      duration,
      timingFunction: easing,
      delay
    })
  }
}

/**
 * 动画预设配置
 */
const AnimationPresets = {
  /**
   * 页面进入动画
   */
  pageEnter: {
    duration: 'normal',
      easing: 'easeOut',
      delay: 'short'
  },
  
  /**
   * 页面退出动画
   */
  pageExit: {
    duration: 'fast',
      easing: 'easeIn',
    delay: 0
  },
  
  /**
   * 模态框动画
   */
  modal: {
    duration: 'normal',
      easing: 'bounce',
    delay: 0
  },
  
  /**
   * 按钮点击动画
   */
  buttonTap: {
    duration: 'fast',
      easing: 'easeOut',
    delay: 0
  },
  
  /**
   * 列表项动画
   */
  listItem: {
    duration: 'normal',
      easing: 'easeOut',
      delay: 'short'
  },
  
  /**
   * 卡片动画
   */
  card: {
    duration: 'normal',
      easing: 'elastic',
      delay: 'medium'
  },
  
  /**
   * 加载动画
   */
  loading: {
    duration: 'slow',
      easing: 'easeInOut',
    delay: 0
  },
  
  /**
   * 通知动画
   */
  notification: {
    duration: 'normal',
      easing: 'bounce',
      delay: 'short'
  }
}

/**
 * 情感动画映射
 */
const EmotionAnimations = {
  happy: {
    enter: () => AnimationUtil.bounce(AnimationConfig.createAnimation(AnimationPresets.card)),
    exit: () => AnimationUtil.zoomOut(AnimationConfig.createAnimation(AnimationPresets.pageExit)),
    idle: () => AnimationUtil.float(AnimationConfig.createAnimation({ duration: 'slow', easing: 'easeInOut' }))
  },
  
  sad: {
    enter: () => AnimationUtil.slideInUp(AnimationConfig.createAnimation(AnimationPresets.pageEnter)),
    exit: () => AnimationUtil.fadeOut(AnimationConfig.createAnimation(AnimationPresets.pageExit)),
    idle: () => AnimationUtil.breathe(AnimationConfig.createAnimation({ duration: 'slow', easing: 'easeInOut' }))
  },
  
  excited: {
    enter: () => AnimationUtil.elasticIn(AnimationConfig.createAnimation({ duration: 'normal', easing: 'elastic' })),
    exit: () => AnimationUtil.zoomOut(AnimationConfig.createAnimation(AnimationPresets.pageExit)),
    idle: () => AnimationUtil.pulse(AnimationConfig.createAnimation({ duration: 'fast', easing: 'easeInOut' }))
  },
  
  calm: {
    enter: () => AnimationUtil.fadeIn(AnimationConfig.createAnimation(AnimationPresets.pageEnter)),
    exit: () => AnimationUtil.fadeOut(AnimationConfig.createAnimation(AnimationPresets.pageExit)),
    idle: () => AnimationUtil.breathe(AnimationConfig.createAnimation({ duration: 'slow', easing: 'easeInOut' }))
  },
  
  angry: {
    enter: () => AnimationUtil.shake(AnimationConfig.createAnimation({ duration: 'normal', easing: 'easeOut' })),
    exit: () => AnimationUtil.slideOutDown(AnimationConfig.createAnimation(AnimationPresets.pageExit)),
    idle: () => AnimationUtil.pulse(AnimationConfig.createAnimation({ duration: 'normal', easing: 'easeInOut' }))
  },
  
  love: {
    enter: () => AnimationUtil.heartbeat(AnimationConfig.createAnimation({ duration: 'normal', easing: 'bounce' })),
    exit: () => AnimationUtil.zoomOut(AnimationConfig.createAnimation(AnimationPresets.pageExit)),
    idle: () => AnimationUtil.heartbeat(AnimationConfig.createAnimation({ duration: 'slow', easing: 'easeInOut' }))
  }
}

/**
 * 动画性能配置
 */
const PerformanceConfig = {
  /**
   * 是否启用动画
   */
  enabled: true,
  
  /**
   * 是否启用复杂动画
   */
  complexAnimations: true,
  
  /**
   * 最大并发动画数量
   */
  maxConcurrentAnimations: 5,
  
  /**
   * 动画帧率
   */
  frameRate: 60,
  
  /**
   * 是否启用硬件加速
   */
  hardwareAcceleration: true,
  
  /**
   * 低性能设备配置
   */
  lowPerformanceMode: {
    enabled: false,
    maxDuration: 200,
    simplifiedEasing: 'ease',
    reducedEffects: true
  }
}

/**
 * 动画性能监控
 */
class AnimationPerformanceMonitor {
  static animationCount = 0
  static startTimes = new Map()
  
  /**
   * 开始动画监控
   */
  static startAnimation(id) {
    this.animationCount++
    this.startTimes.set(id, Date.now())
    
    // 检查并发动画数量
    if (this.animationCount > PerformanceConfig.maxConcurrentAnimations) {
      console.warn(`Too many concurrent animations: ${this.animationCount}`)
    }
  }
  
  /**
   * 结束动画监控
   */
  static endAnimation(id) {
    this.animationCount = Math.max(0, this.animationCount - 1)
    
    const startTime = this.startTimes.get(id)
    if (startTime) {
      const duration = Date.now() - startTime
      this.startTimes.delete(id)
      
      // 记录性能数据
      if (duration > 1000) {
        console.warn(`Long animation detected: ${id} took ${duration}ms`)
      }
    }
  }
  
  /**
   * 获取当前动画数量
   */
  static getCurrentAnimationCount() {
    return this.animationCount
  }
  
  /**
   * 重置监控
   */
  static reset() {
    this.animationCount = 0
    this.startTimes.clear()
  }
}

/**
 * 自适应动画配置
 */
class AdaptiveAnimationConfig {
  static devicePerformance = 'medium'
  
  /**
   * 检测设备性能
   */
  static detectDevicePerformance() {
    const systemInfo = wx.getSystemInfoSync()
    const { model, platform } = systemInfo
    
    // 简单的设备性能检测逻辑
    if (platform === 'ios') {
      this.devicePerformance = 'high'
    } else if (model.includes('iPhone') || model.includes('iPad')) {
      this.devicePerformance = 'high'
    } else {
      this.devicePerformance = 'medium'
    }
  }
  
  /**
   * 获取适配的动画配置
   */
  static getAdaptiveConfig() {
    switch (this.devicePerformance) {
      case 'high':
        return emotionalTheme
      case 'medium':
        return defaultTheme
      case 'low':
        return performanceTheme
      default:
        return defaultTheme
    }
  }
  
  /**
   * 设置设备性能等级
   */
  static setDevicePerformance(level) {
    this.devicePerformance = level
    AnimationConfig.setTheme(this.getAdaptiveConfig())
  }
  
  /**
   * 获取设备性能等级
   */
  static getDevicePerformance() {
    return this.devicePerformance
  }
}

// 初始化自适应配置
AdaptiveAnimationConfig.detectDevicePerformance()
AnimationConfig.setTheme(AdaptiveAnimationConfig.getAdaptiveConfig())

// 注册预设主题
AnimationConfig.registerTheme('default', defaultTheme)
AnimationConfig.registerTheme('emotional', emotionalTheme)
AnimationConfig.registerTheme('performance', performanceTheme)

module.exports = {
  AnimationConfig,
  AnimationPresets,
  EmotionAnimations,
  PerformanceConfig,
  AnimationPerformanceMonitor,
  AdaptiveAnimationConfig,
  defaultTheme,
  emotionalTheme,
  performanceTheme
}